package Heap;

import java.util.PriorityQueue;

public class _502_IPO {
    public int findMaximizedCapital(int k, int W, int[] Profits, int[] Capital) {
        PriorityQueue<int[]> capital = new PriorityQueue<>((a, b) -> a[0] - b[0]);
        PriorityQueue<int[]> profit = new PriorityQueue<>((a, b) -> b[1] - a[1]);
        int len = Capital.length;
        for (int i = 0; i < len; i++) {
            capital.add(new int[]{Capital[i], Profits[i]});
        }

        for (int i = 0; i < k; i++) {
            while (!capital.isEmpty() && capital.peek()[0] <= W) {
                profit.add(capital.poll());
            }
            if (profit.isEmpty()) {
                break;
            }
            W += profit.poll()[1];
        }
        return W;
    }
}
